/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *  
 * @author: jin Chen 
 * @date: Aug 28, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits.dialog;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

public class FontPanel extends javax.swing.JPanel {
    private boolean initFinish;
    private static String fontName[] = {
        "Arial", "Bookman Old Style", "Courier New", "Garamond", "Lucida Sans Typewriter", "Monospaced", "SansSeriff", "Serif", "Symbol", "Tahoma",
        "Times New Roman", "Verdana"
    };
    private static int fontSizes[] = {
        6, 8, 9, 10, 12, 14, 16, 18, 24
    };
   /* private static Hashtable  fontStyle=new Hashtable();
    static{
        fontStyle.put("Plain",new Integer(0));
        fontStyle.put("Bold",new Integer(1));
        fontStyle.put("Italic",new Integer(2));
    }*/
    private static String fontStyleS[] = {
        "Plain",
        "Bold",
        "Italic",
    };
    private static int fontStyle[] = {
        Font.PLAIN ,
        Font.BOLD,
        Font.ITALIC ,
    };

    /** Creates new form JPanel */
    public FontPanel() {
        initComponents();
        initValue();
    }

    public void setPreviewLocation(int loc) {
        if(loc==SwingConstants.EAST) {
            this.mainP.remove(this.previewL);
             this.remove(this.previewL);
            //this.add(previewL,BorderLayout.EAST );

             //previewL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        //previewL.setText("Test");
        //previewL.setBorder(new javax.swing.border.TitledBorder("Preview"));
        previewL.setPreferredSize(new java.awt.Dimension(150, 60));
        java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 0);
        mainP.add(previewL, gridBagConstraints);



        }
        else if(loc==SwingConstants.SOUTH ) {
            this.mainP.remove(this.previewL);
             this.remove(this.previewL);

           java.awt.GridBagConstraints      gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 12, 0);
        mainP.add(previewL, gridBagConstraints);

        }
         this.revalidate();
    }
    private void initValue(){
        for (int i=0;i<this.fontName.length ;i++){

            this.nameCbox.addItem(fontName[i]);
        }
        /*Enumeration keys=fontStyle.keys();
        while (keys.hasMoreElements()) {
            String o = (String) keys.nextElement();
            this.styleCbox.addItem(o);

        }*/
        for (int i=0;i<fontStyleS.length ;i++){
            this.styleCbox.addItem(fontStyleS[i]);
        }
        for (int i=0;i<fontSizes.length ;i++){
            this.sizeCbox.addItem(new Integer(fontSizes[i]));
        }
        this.nameCbox.setSelectedIndex(0);
        this.styleCbox.setSelectedIndex(0);
        this.sizeCbox.setSelectedIndex(4);

        Font f = getSelectedFont();
        previewL.setFont(f);

        initFinish=true;

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        nameCbox = new javax.swing.JComboBox();
        styleCbox = new javax.swing.JComboBox();
        sizeCbox = new javax.swing.JComboBox();
        nameL = new javax.swing.JLabel();
        styleL = new javax.swing.JLabel();
        sizeL = new javax.swing.JLabel();
        mainP=new JPanel();
        mainP.setLayout(new java.awt.GridBagLayout());
        setLayout(new BorderLayout());
       // setLayout(new java.awt.GridBagLayout());

        nameCbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameCboxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        mainP.add(nameCbox, gridBagConstraints);

        styleCbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                styleCboxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        mainP.add(styleCbox, gridBagConstraints);

        sizeCbox.setEditable(true);
        sizeCbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sizeCboxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        mainP.add(sizeCbox, gridBagConstraints);

        nameL.setText("name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        mainP.add(nameL, gridBagConstraints);

        styleL.setText("style");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        mainP.add(styleL, gridBagConstraints);

        sizeL.setText("size");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        mainP.add(sizeL, gridBagConstraints);

        this.previewL=new JLabel();
        previewL.setText("    TEST   ");
        previewL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        previewL.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
        previewL.setBorder(new javax.swing.border.TitledBorder("PreView"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 12, 0);
        mainP.add(previewL, gridBagConstraints);

        this.add(mainP);


    }

    private void styleCboxActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
        setFontForPreview();
    }

    private void nameCboxActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
        setFontForPreview();
    }

    private void sizeCboxActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
        setFontForPreview();
        //System.out.println("select font");
    }

    private void setFontForPreview() {

        if(this.initFinish ){
           Font f = getSelectedFont();
           this.previewL.setFont(f);
             this.firePropertyChange("Font",null,f);
        }

    }

    public Font getSelectedFont(){
        String name=(String) this.nameCbox.getSelectedItem() ;
        String styleS=(String) this.styleCbox.getSelectedItem() ;
        //int style=((Integer)fontStyle.get(styleS)).intValue() ;
        int styleIndex=0;
        if(styleS!=null) {
           styleIndex=this.getIndex(fontStyleS,styleS);
        }
        int style= fontStyle[styleIndex];
        //
        Integer sizeI=(Integer) this.sizeCbox.getSelectedItem() ;
        int sel=sizeCbox.getSelectedIndex();
        int size=6;
        if(sizeI!=null) {
            size=sizeI.intValue() ;
        }
        return new Font(name,style,size);


    }

    public void setSelectedFont(Font font) {
        int style=font.getStyle();
        int size=font.getSize() ;
        String name=font.getName() ;

        int styleI=this.getIndex(fontStyle,style);
        styleCbox.setSelectedIndex(styleI) ;
        int sizeI=this.getIndex(fontSizes,size);
        if(sizeI<0) sizeI=getIndex(fontSizes,14);
        this.sizeCbox.setSelectedIndex(sizeI);
        this.nameCbox.setSelectedItem(name);



    }

    private int getIndex(int[] array,int key){
        Arrays.sort(array);
        return Arrays.binarySearch(array,key);

    }
    private int getIndex(String[] array,String key){
        /* After sort, fontStyleS's index does not match  fontStyle
        Arrays.sort(array);
        return Arrays.binarySearch(array,key);*/
        for (int i=0;i<array.length ;i++){
            if(array[i].equals(key) )
                return i;
        }
        return -1;
    }


    // Variables declaration - do not modify
    private javax.swing.JComboBox styleCbox;
    private javax.swing.JLabel styleL;
    private javax.swing.JComboBox nameCbox;
    private javax.swing.JLabel nameL;
    private javax.swing.JLabel sizeL;
    private javax.swing.JComboBox sizeCbox;
    private JLabel previewL;
    private JPanel mainP;
    // End of variables declaration
   
}

